package lucene;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.index.TermFreqVector;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
//import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.FSDirectory;
//import org.apache.lucene.util.Version;
import org.apache.lucene.util.Version;

public class PaperSearcher {
	private IndexSearcher indexSearcher;
	private IndexReader reader;

	public PaperSearcher(File indexDir) throws Exception {
		IndexReader reader = IndexReader.open(FSDirectory.open(indexDir), true);
		IndexSearcher is = new IndexSearcher(reader);
		this.reader = is.getIndexReader();
		this.indexSearcher = is;
	}

	

	
	public Document retrieveByPid(String pid) throws Exception{
		Term paper = new Term("content_id",pid);
		TermDocs docs = reader.termDocs(paper);
		//int cnt = 0;
		Document doc = null;
		while(docs.next()){
			int id = docs.doc();
			doc = reader.document(id);
			break;
			//cnt++;
		}
		//System.out.println(cnt);
		return doc;
	}
	
	public int retrieveDocByPid(String pid) throws Exception{
		Term paper = new Term("content_id",pid);
		TermDocs docs = reader.termDocs(paper);
		int id = 0;		
		while(docs.next()){
			id = docs.doc();			
			break;			
		}		
		return id;
	}
	
	public HashSet<String> retrieveAuthorsByPid(String pid) throws Exception{
		Term paper = new Term("content_id",pid);
		TermDocs docs = reader.termDocs(paper);
		
		//int cnt = 0;
		Document doc = null;
		while(docs.next()){
			int id = docs.doc();
			doc = reader.document(id);
			break;
			//cnt++;
		}
		if(doc == null)
			return null;
		HashSet<String> authors = new HashSet<String>();
		String auStr = doc.get("author");
		StringTokenizer tokens = new StringTokenizer(auStr);
		while(tokens.hasMoreTokens()) {
			authors.add(tokens.nextToken());
		}
		return authors;
	}
	
	public List<Document> searchByVenue(String venue) throws Exception{
		List<Document> list = new ArrayList<Document>();
		Term conf = new Term("conf", venue);
		TermDocs docs= reader.termDocs(conf);
		while(docs.next()) {
			int id = docs.doc();
			Document doc = reader.document(id);
			list.add(doc);
		}
		System.out.println("results\t" + list.size());
		for(Document d : list) {
			String title = d.get("title");
			String authors = d.get("authors");
			String year = d.get("year");
			System.out.println( year+"\t"+ title + "\t" + authors);
		}
		return list;
	}
	
	public void test() throws Exception{
		int id = 1;
		TermFreqVector vector =  reader.getTermFreqVector(id, "conf");
		String[] terms = vector.getTerms();
		System.out.println("terms num\t" + terms.length);
		for(String str : terms) {			
			System.out.println(str);
		}
		
		
	}		
	
	public ScoreDoc[] retrieveResultsByAuthorId(String aid) throws Exception {
		
		QueryParser parser = new QueryParser(Version.LUCENE_CURRENT,
				"author", new WhitespaceAnalyzer());
		Query query = null;
		TopScoreDocCollector collector = TopScoreDocCollector.create(100000,
				false);
		String searchStr = "author:" + aid;
		query = parser.parse(searchStr);
		indexSearcher.search(query, collector);
		ScoreDoc[] results = collector.topDocs().scoreDocs;
		
		return results;
	}
	

	public IndexReader getReader() {
		return reader;
	}

	public static void main(String[] args) throws Exception {
      /* 
		String user = "1";
		File indexDir = new File(
				"D:\\CityU\\project\\bibsonomy\\experiment\\"
				+ user + "\\testIndex\\");

		PaperSearcher searcher = new PaperSearcher(indexDir);
        IndexReader reader = searcher.getReader();
		String pid = "712491";
		Document doc =  reader.document(searcher.retrieveDocByPid(pid));
		String title = doc.get("content");
		System.out.println(pid + "\t" + title);

		//String venue = "SIGMOD Conference";
	//	searcher.searchByVenue(venue);
		*/
		
		File indexDir = new File("D:\\CityU\\project\\bibsonomy\\"
				+ "\\BibIndex\\");
		PaperSearcher searcher = new PaperSearcher(indexDir);
		String pid = "5155890";
		String text = searcher.retrieveByPid(pid).get("author");
		System.out.println(text);
		
		
		/*
		String aid = "18932"; //jiawei han 18932
		ScoreDoc[] results = searcher.retrieveResultsByAuthorId(aid);
		System.out.println(aid + "\tnum\t" + results.length);
		*/
		
	}
}
